﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditSlideShow : System.Web.UI.UserControl
    {
        public string imageslide;
        public string nameslide;
        public string link;
        public string title;
        public int slideid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                slideid = Convert.ToInt32(Request.QueryString["idslide"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Banner", "*", "Banner_ID", slideid, "selectwhereid");
                imageslide = dt.Rows[0]["Banner_Image"].ToString();
                link = dt.Rows[0]["Banner_Link"].ToString();
                title = dt.Rows[0]["Banner_Title"].ToString();
                nameslide = dt.Rows[0]["Banner_Name"].ToString();
                txtLink.Text = link;
                txtTitle.Text = title;
                txtName.Text = nameslide;

            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {

            if (txtLink.Text != "")
            {
                if (txtTitle.Text != "")
                {
                    if (fulImageSlide.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageSlide.FileName))
                        {
                            string linkimage = fulImageSlide.FileName;
                            LIB.Validate saveimage = new LIB.Validate();
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Slide-Show/"), linkimage, 940, fulImageSlide.PostedFile.InputStream);
                            filePathSlide = "/Images/Slide-Show/" + fulImageSlide.FileName;

                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                            cl[0] = new DAL.DHelpers.Column("Banner_Name", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Banner_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Banner_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Banner_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Banner_ID";

                            object[] colValue = new object[1];
                            colValue[0] = slideid;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Banner", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                        cl[0] = new DAL.DHelpers.Column("Banner_Link", txtLink.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                        cl[1] = new DAL.DHelpers.Column("Banner_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                        string[] colWhere = new string[1];
                        colWhere[0] = "Banner_ID";

                        object[] colValue = new object[1];
                        colValue[0] = slideid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_Banner", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=5&sub=0");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên tiêu đề slideshow...')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập link liên kết...')</script>");
            }
        }
    }
}